/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.pbp.model;

import ac.pbp.core.util.MyValidator;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;

/**
 *
 * @author Kim
 */
@Entity
@Table(name = "buku")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Buku implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @Column(name = "kode_buku")
    private String kodeBuku;
    
    @Column(name = "nama_buku")
    private String namaBuku;
    
    @Column(name = "desc_buku")
    private String descBuku;
        
    @Column(name = "kode_genre")
    private String kodeGenre;
        
    @Column(name = "cover")
    private String cover;
        
    @Column(name = "tgl_insert")
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date tglInsert;
        
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "kode_pengarang")
    @LazyToOne(LazyToOneOption.NO_PROXY)
    @Fetch(FetchMode.SELECT)
    private Pengarang pengarang;
    
    @OneToMany(mappedBy="buku",fetch=FetchType.LAZY,cascade=CascadeType.ALL)
    @LazyCollection(LazyCollectionOption.EXTRA)
    @Fetch(FetchMode.SELECT)
    private Set<BukuUser> bukuUserList = new HashSet<BukuUser>();

    public String getKodeBuku() {
        return kodeBuku;
    }

    public void setKodeBuku(String kodeBuku) {
        this.kodeBuku = kodeBuku;
    }

    public String getNamaBuku() {
        return namaBuku;
    }

    public void setNamaBuku(String namaBuku) {
        this.namaBuku = namaBuku;
    }

    public String getDescBuku() {
        return descBuku;
    }

    public void setDescBuku(String descBuku) {
        this.descBuku = descBuku;
    }

    public String getKodeGenre() {
        return kodeGenre;
    }

    public void setKodeGenre(String kodeGenre) {
        this.kodeGenre = kodeGenre;
    }

    public Pengarang getPengarang() {
        return pengarang;
    }

    public void setPengarang(Pengarang pengarang) {
        this.pengarang = pengarang;
    }

    public String getCover() {
        return cover;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }

    public Set<BukuUser> getBukuUserList() {
        return bukuUserList;
    }

    public void setBukuUserList(Set<BukuUser> bukuUserList) {
        this.bukuUserList = bukuUserList;
    }

    public Date getTglInsert() {
        return tglInsert;
    }

    public void setTglInsert(Date tglInsert) {
        this.tglInsert = tglInsert;
    }

    public static final Pengarang newInstance(String id) {
        if (MyValidator.validateParam(id)) {
            Pengarang pengarang = new Pengarang();
            pengarang.setKodePengarang(id);

            return pengarang;
        }
        return null;
    }
}
